package com.lanbu.chat.service.impl;
import java.util.List;
import com.lanbu.common.annotation.DataScope;
import com.lanbu.common.core.domain.entity.SysUser;
import com.lanbu.common.exception.ServiceException;
import com.lanbu.common.utils.SecurityUtils;
import com.lanbu.common.utils.StringUtils;
import com.lanbu.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lanbu.chat.mapper.ImGroupMapper;
import com.lanbu.chat.domain.ImGroup;
import com.lanbu.chat.service.IImGroupService;

/**
 * 群Service业务层处理
 *
 * @author lanbu
 * @date 2024-09-15
 */
@Service
public class ImGroupServiceImpl implements IImGroupService
{
    @Autowired
    private ImGroupMapper imGroupMapper;


    /**
     * 校验群聊是否有数据权限
     *
     * @param groupId 群聊id
     */
    @Override
    public void checkGroupDataScope(Long groupId)
    {
        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
        {
            ImGroup imGroup = new ImGroup();
            imGroup.setId(groupId);
            List<ImGroup> imGroups = SpringUtils.getAopProxy(this).selectImGroupList(imGroup);
            if (StringUtils.isEmpty(imGroups))
            {
                throw new ServiceException("没有权限访问群聊数据！");
            }
        }
    }

    @Override
    public ImGroup selectImGroupById(Long id)
    {
        return imGroupMapper.selectImGroupById(id);
    }

    @Override
    @DataScope(deptAlias = "u",userAlias = "u")
    public List<ImGroup> selectImGroupList(ImGroup imGroup)
    {
        return imGroupMapper.selectImGroupList(imGroup);
    }

    @Override
    public int insertImGroup(ImGroup imGroup)
    {
        return imGroupMapper.insertImGroup(imGroup);
    }

    @Override
    public int updateImGroup(ImGroup imGroup)
    {
        return imGroupMapper.updateImGroup(imGroup);
    }

    @Override
    public int deleteImGroupByIds(Long[] ids)
    {
        for (Long id : ids) {
            checkGroupDataScope(id);
        }
        return imGroupMapper.deleteImGroupByIds(ids);
    }

    @Override
    public int deleteImGroupById(Long id)
    {
        return imGroupMapper.deleteImGroupById(id);
    }
}
